<?php

namespace App\Http\Controllers\Activity;

use App\Http\Controllers\Controller;
use App\Models\Activity\Goods;
use App\Models\Tools\AliOss;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class GoodsStoreController extends Controller
{
    /**
     * 查询所有活动
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){

        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;

        $where = function ($query) use ($all) {
            if(isset($all['openid'])){
                $query->where('lu.openid','=',$all['openid']);
            }
            if(isset($all['nickname'])){
                $query->where('lu.nickname','like','%'.$all['nickname'].'%');
            }
            if(isset($all['goods_id'])){
                $query->where('lg.goods_id','=',$all['goods_id']);
            }
            if(isset($all['goods_name'])){
                $query->where('lg.goods_name','like','%'.$all['goods_name'].'%');
            }
        };

        $total = DB::table('lottery_goods as lg')
            ->leftJoin('lottery_users as lu','lg.creator','=','lu.we_id')
            ->where($where)
            ->count();

        $result = DB::table('lottery_goods as lg')
            ->leftJoin('lottery_users as lu','lg.creator','=','lu.we_id')
            ->leftJoin('shop','lg.shop_id','=','shop.shop_id')
            ->where($where)
            ->orderByDesc('goods_id')
            ->skip(($pageNo-1)*$pageSize)
            ->take($pageSize)
            ->select('lg.goods_id','lg.goods_name','lg.title_img','lg.price','lg.goods_img','lg.detail','lg.is_delete','lg.creator',
                'lu.nickname','lu.openid','shop.shop_id','shop.shop_name')
            ->get()
            ->toArray();

        foreach ($result as $index=>$item) {
            $item->title_img = AliOss::getUrl($item->title_img);
            $item->goods_img = AliOss::getUrls(explode(',',$item->goods_img));
        }

        return $this->setReturnJson(0,'success',['info'=>$result,'total'=>$total]);

    }

}
